package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import CommonGui.NotEdditibleTable;
import DataObjects.ConstantObjects;
import DataObjects.StandardMenuLeftList;
import DataObjects.StandardMenuRightList;
import Messages.MessageAddNewBusinessMenu;
import Messages.MessageAddNewStandardMenu;
import Messages.MessageGetAllRecipes;
import Messages.MessageGetAllRecipesReply;
import Messages.MessageShowAllStandardsMenus;
import Messages.MessageShowAllStandardsMenusReply;
import Messages.MessageShowIDMenu;
import Messages.MessageShowIDStandardMenu;
import Messages.MessageShowIdMenuReply;
import Messages.MessageShowIdStandardMenuReply;

public class PFAddNewStandartMenu extends JPanel{
	private JPanel totalPanel;
	private JPanel bulkiPanel;
	private int count_res_menuRecipes=0;
	private int mId;
	private String flag;     
	private String standard_menu="standard";     //flag that show in server that this is standard menu and so it puts all details of new menu in recipe_in_menu table
	private JComboBox statusMenu;
	
	private JTextField recipeTxt;
//	private JTextField roleTxt;
	private JTextField weightTxt;
	private JTextField	costTxt;
	private JTextField textField_MenuId;
	private JTextField	textField_nameMenu;
	private JTextField textField_price;
	private JTextField priceItemTxt;
	private JTextField constCostItemTxt;
	private JTextField nameTF1;
	private JTextField nameTF2;
	
	
	private String menuId;
	private MainMenu mainMenu;
	
	private JPanel leftPanel;
	private JPanel rightPanel;
	private JPanel detailsPanel;
	private JPanel tablePanel;
	private JPanel allItemsPanel;
	private JPanel middlePanel;
	private JPanel suppItemsPanel;
	private JPanel  allRecipesListPanel;
	private JPanel allRecipesPanelSearch;
	private JPanel menuRecipesPanelSearch;
	private JPanel menuRecipeListPanel;
	private JPanel buttonsPanel;
	private JPanel costMenuPanel;
	
	private JList allRecipesList;
	private JList menuRecipesList;
	private DefaultListModel allRecipeModel;
	private DefaultListModel menuRecipesModel;
	
	private Vector<StandardMenuLeftList> res_allRecipes;                 
	private Vector<StandardMenuRightList> res_menuRecipes; 
	private Vector<StandardMenuLeftList> res_allRecipesTemp;
	private Vector <String> tblheader;
	
	private JButton leftButton;
	private JButton rightButton;
	
	private JTable tbl;
	private DefaultTableModel dtm;
	private Component horizontalGlue;
	
	DefaultTableCellRenderer centerRenderer;
	
	public PFAddNewStandartMenu (MainMenu mm)
	{
		super();
		this.mainMenu=mm;
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		
		initialize();
	}

	
	private void initialize()
	{
		add(getLeftPanel());
		add(getRightPanel());
		
	}
	
	//====================== panels for leftPanel	========================================
	
	private JPanel getLeftPanel()            //search by category panel and choose specific item panel 
	{
		
		leftPanel=new JPanel();
		leftPanel.setBorder(new TitledBorder(null, "בניית תפריט רגיל חדש", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
		leftPanel.add(getBulkiPanel());
		leftPanel.add(getCostMenuPanel());
		return leftPanel;
	}
	
	private JPanel	getCostMenuPanel()
	{
		
		costMenuPanel=new JPanel();
		GridBagLayout gbll = new GridBagLayout();    
		costMenuPanel.setLayout(gbll);
		GridBagConstraints gbc1 = new GridBagConstraints();
		GridBagConstraints gbc2 = new GridBagConstraints();
		
		gbc1.ipadx = 5;
		gbc1.ipady =15;
		
		
		
		gbc1.gridheight=1;
		gbc1.gridwidth=0;
		gbc1.insets = new Insets(0,700,0,0);
		
		gbc2.gridheight=1;
		gbc2.gridwidth=0;
		gbc2.insets = new Insets(0,700,0,0);
		
		JLabel tWeightLbl=new JLabel("סהכ:");
		//costMenuPanel.add(tWeightLbl,gbc1);
		
		//costMenuPanel.add(getCostTxt(),gbc1);
		
	return costMenuPanel;
		
	}
	
	
	
	/*private JTextField getCostTxt()
	{
		costTxt=new JTextField();
		costTxt.setHorizontalAlignment(JTextField.RIGHT);
		costTxt.setColumns(10);
	return costTxt;
	}*/
	
	
	private JPanel getBulkiPanel()
	{
		
		bulkiPanel=new JPanel();
		
		bulkiPanel.setLayout(new BoxLayout(bulkiPanel, BoxLayout.X_AXIS));
		bulkiPanel.add(getAllRecipesPanel());        //panel with searchPanel(searchTxt+searchLbl) + listPanel(list with all recipes) 
		bulkiPanel.add(getMiddlePanel());;         // panel with right and left buttons + textfield for recipe + textfield for role + textfield for Weight
		bulkiPanel.add(getMenuRecipesPanel());
	return bulkiPanel;
	}
	
	
	
	
	private JPanel getAllRecipesPanel()
	{
		
		allItemsPanel=new JPanel();
		allItemsPanel.setLayout(new BoxLayout(allItemsPanel, BoxLayout.Y_AXIS));
		allItemsPanel.add(getAllRecipesPanelSearch());
		allItemsPanel.add(getListAllRecipesPanel());		
	return allItemsPanel;

	}
	
	
	
	private JPanel getListAllRecipesPanel ()
	{
		
		allRecipesListPanel=new JPanel();
		allRecipesListPanel.setLayout(new BoxLayout(allRecipesListPanel, BoxLayout.X_AXIS));	
		allRecipeModel=new DefaultListModel();
		MessageGetAllRecipes allRecipes=new MessageGetAllRecipes();
		try {
			mainMenu.ms.client.sendToServer(allRecipes);
			MessageGetAllRecipesReply result = (MessageGetAllRecipesReply)mainMenu.ms.client.GetMessage();
			this.res_allRecipes=result.getRecipes();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		int size_vector=res_allRecipes.size();		
		for(int i=0;i<size_vector;i++) 
		{
			allRecipeModel.addElement((String) res_allRecipes.get(i).getName());  
		}
		
		allRecipesList = new JList(allRecipeModel);		
		allRecipesList.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		allRecipesList.setFont(new Font("Tahoma", Font.PLAIN, 14));
		allRecipesList.setBackground(new Color(230, 230, 250));
		JScrollPane listScroller1 = new JScrollPane(allRecipesList);
		listScroller1.setPreferredSize(new Dimension(200, 600));
		allRecipesListPanel.add(listScroller1);
		
		String selectedItem=(String)allRecipesList.getSelectedValue();
		
		allRecipesList.addListSelectionListener(new ListSelectionListener() 
	 	{
	 	      public void valueChanged(ListSelectionEvent evt) {
	 	        if (evt.getValueIsAdjusting())
	 	        return;
	 	      	     
	 	      JList itemList=(JList) evt.getSource();
	          String value=(String) itemList.getSelectedValue();
	          int index=allRecipesList.getSelectedIndex();
	          menuRecipesList.clearSelection();
	          allRecipesList.setSelectedIndex(index);
	          for(int i=0;i<res_allRecipes.size();i++) 
		  		{
		        	  
		        	  if(value==(null))
		        		  break;
		  			if(value.equals(res_allRecipes.get(i).getName()))
		  			{
		  				recipeTxt.setText(value);
		  				weightTxt.setText(Float.toString(res_allRecipes.get(i).getMishkal()));  //mishkal
		  				costTxt.setText(Float.toString(res_allRecipes.get(i).getCost()));    //cost
		  				constCostItemTxt.setText(Float.toString(res_allRecipes.get(i).getConstCost()));  //constant cost
		  					 	       
		  			}
		  		}	
	          
	         
	 	       
	 	   
	 	      }
	 	    });
	
		return allRecipesListPanel;
	}
	
	/**
	 * Method for creation JText filters to quick searching of recipes by name
	 * @return  JPanel allRecipesPanelSearch
	 * 
	 */
	private JPanel getAllRecipesPanelSearch()           //Panel dlya sadot lehipus
	{
		
		
		allRecipesPanelSearch=new JPanel();
		allRecipesPanelSearch.setLayout(new BoxLayout(allRecipesPanelSearch, BoxLayout.X_AXIS));
		JTextField searchTxt=new JTextField();
		searchTxt.setHorizontalAlignment(JTextField.RIGHT);
		JLabel searchLbl=new JLabel(":חיפוש לפי שם המתכון");
		allRecipesPanelSearch.add(getNameTF1());
		allRecipesPanelSearch.add(searchLbl);
		return allRecipesPanelSearch;
	}
	
	private JTextField getNameTF1()
	{
		nameTF1=new JTextField(20);
		nameTF1.setHorizontalAlignment(JTextField.RIGHT);
	
		KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				JTextField nameField = (JTextField)arg0.getSource();
				String selectedName = (String)nameField.getText();
				filterByName1(selectedName);				
			}

			public void keyTyped(KeyEvent arg0) {}		
		};
		
		nameTF1.addKeyListener(keyListener);		
		return nameTF1;
	}
	
	//filter for all items list
	private void filterByName1(String str)
	{
		allRecipeModel.clear();
		for(int i=0;i<res_allRecipes.size();i++)
		{
			if(((String)res_allRecipes.get(i).getName()).startsWith(str))
				allRecipeModel.addElement((String) res_allRecipes.get(i).getName());  
		}
	}
	
	private JPanel getMenuRecipesPanel()               //Pravaya bulka + sadot le hipus 
	{
		
		suppItemsPanel=new JPanel();
		suppItemsPanel.setLayout(new BoxLayout(suppItemsPanel, BoxLayout.Y_AXIS));
		suppItemsPanel.add(getMenuRecipesPanelSearch());
		suppItemsPanel.add(getListMenuRecipePanel());
		return suppItemsPanel;

	}
	
	
	
	
	
	
	private JPanel getListMenuRecipePanel ()          //Pravaya bulka  
	{
		
		
		menuRecipeListPanel=new JPanel();
		menuRecipeListPanel.setLayout(new BoxLayout(menuRecipeListPanel, BoxLayout.X_AXIS));	
		menuRecipesModel=new DefaultListModel();
		menuRecipesList = new JList(menuRecipesModel);	
		menuRecipesList.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		menuRecipesList.setFont(new Font("Tahoma", Font.PLAIN, 14));
		menuRecipesList.setBackground(new Color(230, 230, 250));
		JScrollPane listScroller2 = new JScrollPane(menuRecipesList);
		listScroller2.setPreferredSize(new Dimension(200, 600));
		menuRecipeListPanel.add(listScroller2);
		
		menuRecipesList.addListSelectionListener(new ListSelectionListener() 
	 	{
	 	      public void valueChanged(ListSelectionEvent evt) {
	 	        if (evt.getValueIsAdjusting())
	 	        return;
	 	      	     
	 	      JList itemList=(JList) evt.getSource();
	          String value=(String) itemList.getSelectedValue();
	          int index=menuRecipesList.getSelectedIndex();
              allRecipesList.clearSelection();
	          menuRecipesList.setSelectedIndex(index);
	          
	          while(count_res_menuRecipes<res_menuRecipes.size()){
	        	  
           	   if(value==(String)res_menuRecipes.get(count_res_menuRecipes).getName()){
           		recipeTxt.setText(value);   
           		weightTxt.setText((String) res_menuRecipes.get(count_res_menuRecipes).getMishkal());  
           		costTxt.setText(Float.toString( res_menuRecipes.get(count_res_menuRecipes).getCost()));  
           		constCostItemTxt.setText(Float.toString( res_menuRecipes.get(count_res_menuRecipes).getConstCost()));  
           		priceItemTxt.setText(Integer.toString( res_menuRecipes.get(count_res_menuRecipes).getPrice()));  
           		
           		
           	   }
	          count_res_menuRecipes++;
	          }
	          count_res_menuRecipes=0;
	 	      mainMenu.validate();
	 	      }
	 	    });
		
	return menuRecipeListPanel;
	}
	     
	/**
	 * Method for creation JTextFields filters to quick searching of recipes by name
	 * @return  JPanel menuRecipesPanelSearch
	 * 
	 */
	private JPanel getMenuRecipesPanelSearch()                    ////Panel dlya sadot lehipus
	{
		
		menuRecipesPanelSearch=new JPanel();
		menuRecipesPanelSearch.setLayout(new BoxLayout(menuRecipesPanelSearch, BoxLayout.X_AXIS));
		JTextField searchTxt=new JTextField();
		searchTxt.setHorizontalAlignment(JTextField.RIGHT);
		JLabel searchLbl=new JLabel("חיפוש לפי שם המוצר :");
		menuRecipesPanelSearch.add(getNameTF2());
		menuRecipesPanelSearch.add(searchLbl);
		
	return menuRecipesPanelSearch;
	}
	
	private JTextField getNameTF2()
	{
		nameTF2=new JTextField(20);
		nameTF2.setHorizontalAlignment(JTextField.RIGHT);
	
		KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				JTextField nameField = (JTextField)arg0.getSource();
				String selectedName = (String)nameField.getText();
				filterByName2(selectedName);				
			}

			public void keyTyped(KeyEvent arg0) {}		
		};
		
		nameTF2.addKeyListener(keyListener);		
		return nameTF2;
	}
	
	//filter for all items list
	private void filterByName2(String str)
	{
		menuRecipesModel.clear();
		for(int i=0;i<res_menuRecipes.size();i++)
		{
			if(((String)res_menuRecipes.get(i).getName()).startsWith(str))
				menuRecipesModel.addElement((String) res_menuRecipes.get(i).getName());  
		}
	}
	
	private JPanel getMiddlePanel()                                 //Panel with buttons "Right" "Left"
	{
		
		middlePanel=new JPanel();
		
		
		GridBagLayout gbl = new GridBagLayout();    
		middlePanel.setLayout(gbl);
		GridBagConstraints gbc1 = new GridBagConstraints();
		GridBagConstraints gbc2 = new GridBagConstraints();
		GridBagConstraints gbc3 = new GridBagConstraints();
		GridBagConstraints gbc4 = new GridBagConstraints();
		GridBagConstraints gbc5 = new GridBagConstraints();
		GridBagConstraints gbc6 = new GridBagConstraints();
		GridBagConstraints gbc7 = new GridBagConstraints();
		GridBagConstraints gbc8 = new GridBagConstraints();
		GridBagConstraints gbc9 = new GridBagConstraints();
		GridBagConstraints gbc10 = new GridBagConstraints();
		GridBagConstraints gbc11 = new GridBagConstraints();
		GridBagConstraints gbc12 = new GridBagConstraints();
		
		gbc1.fill=GridBagConstraints.HORIZONTAL;
		gbc1.gridheight=1;
		gbc1.gridwidth=0;
		gbc1.insets = new Insets(0,0,50,0);
		
		gbc4.fill=GridBagConstraints.HORIZONTAL;
		gbc4.gridheight=1;
		gbc4.gridwidth=0;
		gbc4.insets = new Insets(0,0,50,0);
	
		
		gbc2.gridheight=1;
		gbc2.gridwidth=0;
		gbc2.insets = new Insets(0,0,0,0);
		
		gbc3.gridheight=1;
		gbc3.gridwidth=0;
		gbc3.insets = new Insets(0,0,20,0);
		
		gbc6.gridheight=1;
		gbc6.gridwidth=0;
		gbc6.insets = new Insets(0,0,0,0);
		
				
		gbc5.gridheight=1;
		gbc5.gridwidth=0;
		gbc5.insets = new Insets(0,0,20,0);
		
		gbc7.gridheight=1;
		gbc7.gridwidth=0;
		gbc7.insets = new Insets(0,0,0,0);
		
		gbc8.gridheight=1;
		gbc8.gridwidth=0;
		gbc8.insets = new Insets(0,0,20,0);
		
		gbc9.gridheight=1;
		gbc9.gridwidth=0;
		gbc9.insets = new Insets(0,0,0,0);
		
		gbc10.gridheight=1;
		gbc10.gridwidth=0;
		gbc10.insets = new Insets(0,0,0,0);
		
		gbc11.gridheight=1;
		gbc11.gridwidth=0;
		gbc11.insets = new Insets(0,0,0,0);
		
		gbc12.gridheight=1;
		gbc12.gridwidth=0;
		gbc12.insets = new Insets(0,0,20,0);
		
		middlePanel.add(getRightButton(),gbc1);


		middlePanel.add(getLeftButton(),gbc4);
		
					
		JLabel itemLbl=new JLabel("מתכון:");
		middlePanel.add(itemLbl,gbc6);
		middlePanel.add(getRecipeTxt(),gbc5);	
		
		
		JLabel tWeightLbl=new JLabel("משקל:");
		middlePanel.add(tWeightLbl,gbc2);
		
		middlePanel.add(getWeightTxt(),gbc3);
		
		JLabel costLbl=new JLabel("עלות:");
		middlePanel.add(costLbl,gbc7);
		
		middlePanel.add(getCostTxt(),gbc8);
		
		JLabel constCostLbl=new JLabel("הוצאות נלוואות :");
		middlePanel.add(constCostLbl,gbc11);
		middlePanel.add(getConstCostItemTxt(),gbc12);
		
		
		JLabel priceLbl=new JLabel("מחיר :");
		middlePanel.add(priceLbl,gbc9);
		middlePanel.add(getPriceItemTxt(),gbc10);
		
		
		
	return middlePanel;
	}
	
	private JTextField getPriceItemTxt()
	{
		priceItemTxt=new JTextField();
		priceItemTxt.setHorizontalAlignment(JTextField.RIGHT);
		priceItemTxt.setColumns(10);
		

	return priceItemTxt;
	}
	
	private JTextField getConstCostItemTxt()
	{
		constCostItemTxt=new JTextField();
		constCostItemTxt.setHorizontalAlignment(JTextField.RIGHT);
		constCostItemTxt.setColumns(10);
		constCostItemTxt.setEditable(false);
		

	return constCostItemTxt;
	}
	
	
	
	
	private JTextField getCostTxt()
	{
		costTxt=new JTextField();
		costTxt.setHorizontalAlignment(JTextField.RIGHT);
		costTxt.setColumns(10);
		costTxt.setEditable(false);
		
	return costTxt;
	}
	
	
	private JTextField getRecipeTxt()
	{
		recipeTxt=new JTextField();
		recipeTxt.setHorizontalAlignment(JTextField.RIGHT);
		recipeTxt.setColumns(10);
		recipeTxt.setEditable(false);

	return recipeTxt;
	}
	
	
	private JTextField getWeightTxt()
	{
		weightTxt=new JTextField();
		weightTxt.setHorizontalAlignment(JTextField.RIGHT);
		weightTxt.setColumns(10);
		weightTxt.setEditable(false);

	return weightTxt;
	}
	
	
	private JButton getRightButton()
	{
		rightButton=new JButton("--->");
		res_menuRecipes = new Vector<StandardMenuRightList>();
		
		rightButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
			String selectedItem=(String)allRecipesList.getSelectedValue();
			res_allRecipesTemp=new Vector<StandardMenuLeftList>();
				if (selectedItem!=null)
				{
					if(priceItemTxt.getText().isEmpty()){
			    		 JOptionPane.showMessageDialog(null,"יש לתת מחיר למנה");
			    				 }
					else{
						
					allRecipeModel.removeElement(selectedItem);
					menuRecipesModel.addElement(selectedItem);
					for(int i=0; i<res_allRecipes.size();i++)
					{
					     if(res_allRecipes.get(i).getName().equals(selectedItem))
					     {
					    	 	    	 
					    	
					    	 StandardMenuRightList rightList = new StandardMenuRightList();
					    						    	
					    	 
					    	 rightList.setName(res_allRecipes.get(i).getName());
					    	 rightList.setIsSelectable(1);
					    	 rightList.setPrice(Integer.parseInt(priceItemTxt.getText()));
					    	 rightList.setQuantity(1);
					    	 rightList.setRid(res_allRecipes.get(i).getRid());
					    	 rightList.setCost(Float.parseFloat(costTxt.getText()));
					    	 rightList.setMishkal(weightTxt.getText());
					    	 rightList.setConstCost(Float.parseFloat(constCostItemTxt.getText()));
					    	 
					    	 res_menuRecipes.add(rightList);
					    	 
					    		recipeTxt.setText("");
					    		weightTxt.setText("");
					    		costTxt.setText("");
					    		constCostItemTxt.setText("");
					    		priceItemTxt.setText("");
					    		
					    		
					    		NotEdditibleTable orderModel = (NotEdditibleTable) (tbl
										.getModel());
					    		orderModel.setDataVector(parseVectorObjectToVectorVector(res_menuRecipes), tblheader);
					    		tbl.setModel(orderModel);
					    		for(int x=0;x<4;x++){
									tbl.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
							        }
					    	    res_allRecipesTemp.add(res_allRecipes.get(i));
					    	    res_allRecipes.remove(i);
					    	 break;
					     					    	
					     }
					    
														    		 
					     }
					     
					}
				
				} //if (selectedItem!=null)
				
				else
				{
					JOptionPane.showMessageDialog(null,"יש לבחור פריט");
				}
						
			}
		});
	
		
		return rightButton;		
	}
	
	private Vector<Vector> parseVectorObjectToVectorVector (Vector<StandardMenuRightList> vectorObj)
	{
		Vector<Vector> vectorvector=new Vector<Vector>();
		for(int i=0;i<vectorObj.size();i++)
		{
			Vector<String> vector=new Vector<String>();
			vector.add(vectorObj.get(i).getName());
		    vector.add(Integer.toString((Integer)(vectorObj.get(i).getPrice())));
		    vector.add(Float.toString(vectorObj.get(i).getCost()));
			vector.add(vectorObj.get(i).getMishkal());
			
			vectorvector.add(vector);
		}
		
		return vectorvector;
		
		
		
	}
	
	private JButton getLeftButton()
	{
		leftButton=new JButton("<---");
		
		leftButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
				String selectedItem=(String)menuRecipesList.getSelectedValue();
				if (selectedItem!=null)
				{
					menuRecipesModel.removeElement(selectedItem);
					allRecipeModel.addElement(selectedItem);
					for(int i=0; i<res_menuRecipes.size();i++)
					{
					     if(res_menuRecipes.get(i).getName().equals(selectedItem))
					     {
					    	 for(int j=0;j<res_allRecipesTemp.size();j++)
					    	 {
					    		 if(res_allRecipesTemp.get(j).getName().equals(selectedItem))
					    		 {
					    			 
					    			 res_allRecipes.add(res_allRecipesTemp.get(j));
					    			 res_allRecipesTemp.remove(j);
					    		 }
					    		             break;     
					    	 }
					    	        res_menuRecipes.remove(i);
					    	 		recipeTxt.setText("");
						    		weightTxt.setText("");
						    		costTxt.setText("");
						    		constCostItemTxt.setText("");
						    		priceItemTxt.setText("");
					    	 
						    		NotEdditibleTable orderModel = (NotEdditibleTable) (tbl
											.getModel());
						    		orderModel.setDataVector(parseVectorObjectToVectorVector(res_menuRecipes), tblheader);
						    		tbl.setModel(orderModel);
						    		for(int x=0;x<4;x++){
										tbl.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
								        }
					    	 break;
					     }
					}
				} 
				
				else
				{
					JOptionPane.showMessageDialog(null,"יש לבחור פריט");
				}
						
			}
		});
		
		return leftButton;		
	}
	
	
	//=====================panels for rightPanel ============================================	
	
	private JPanel getRightPanel()
	{
		rightPanel=new JPanel();
		rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
		rightPanel.add(getDetailsPanel());            // panel with buttons for getting details for new menu
		rightPanel.add(getTablePanel());             // panel with recipes table
		rightPanel.add(getButtonsPanel());           // panel with save and cancel buttons 
		
	return rightPanel;
	}
	
	
	private JPanel getTablePanel()
	{
		
		tablePanel=new JPanel();
		tablePanel.setBorder(new TitledBorder(null, "המתכונים המשתתפים בתפריט", TitledBorder.CENTER, TitledBorder.TOP, null, null));
	    tblheader=new Vector<String>();
		tblheader.add("מתכון");
		tblheader.add("מחיר");
		tblheader.add("עלות");
		tblheader.add("משקל");
		
		
		
		dtm=new NotEdditibleTable(parseVectorObjectToVectorVector(res_menuRecipes),tblheader);	 	 		
		tbl = new JTable(dtm);
	    centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		for(int x=0;x<4;x++){
			tbl.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
	        }
				
		tbl.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
				
		tbl.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		tbl.setFont(new Font("David", Font.BOLD, 16));
		tbl.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 17 ));
		tbl.getTableHeader().setBackground(new Color(173,216,230));
		tbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
		
	    tbl.setRowHeight(30);
		tbl.setFillsViewportHeight(true);
        tbl.setGridColor(new Color(25, 25, 112));
        tbl.setBackground(new Color(255, 240, 245));
        tbl.setForeground(new Color(0, 0, 0));
        tbl.setAutoCreateRowSorter(true);
        
        JScrollPane scrollPane = new JScrollPane(tbl);
        tablePanel.add(scrollPane);
		
	return tablePanel;
	}
	
	private JPanel getButtonsPanel()
	{
		
		buttonsPanel=new JPanel();
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
		buttonsPanel.add(getSaveButton());
		buttonsPanel.add(Box.createHorizontalStrut(25));
		buttonsPanel.add(getCancelButton());
		
	return buttonsPanel;
		
	}
	
	private JButton getSaveButton()
	{
		
	
		JButton saveButton=new JButton("שמור");
		saveButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
				int result1;
				result1=inputCheck();		
				if(result1==0)
				{
					
							 Vector<String>	menuDetails = new Vector<String>();
							menuDetails.add(textField_MenuId.getText());							
							menuDetails.add(textField_nameMenu.getText());							
							menuDetails.add(flag);
							menuDetails.add(flag);
							menuDetails.add(standard_menu);
							
					
						
					
							MessageAddNewStandardMenu msgNewStandardMenu= new MessageAddNewStandardMenu(res_menuRecipes,menuDetails);
					try {
						
						mainMenu.ms.client.sendToServer(msgNewStandardMenu);
						
						
						
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				
			}
		});
	return saveButton;
	}
	
	private JButton getCancelButton()
	{
		final Vector<String> arr1 = new Vector<String>();
		JButton cancelButton=new JButton("ביטול");
		cancelButton.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				MessageShowAllStandardsMenus msgShowMenu=new MessageShowAllStandardsMenus( );
				
				try {
					mainMenu.ms.client.sendToServer(msgShowMenu);
					MessageShowAllStandardsMenusReply resultMenu=(MessageShowAllStandardsMenusReply)mainMenu.ms.client.GetMessage();
				mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
				mainMenu.getContentPane().add(new PfSearch_standard_menu(mainMenu,resultMenu.getResultMenu()), BorderLayout.CENTER);	
				mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
				mainMenu.getContentPane().add(new PanelFunctionMenuAllStandartsMenu(mainMenu,arr1), BorderLayout.EAST);
				mainMenu.getContentPane().validate();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	return cancelButton;
	}
	
	
	private JPanel getDetailsPanel()
	{
		detailsPanel=new JPanel();
		detailsPanel.setBorder(new TitledBorder(null, "פרטים של תפריט הרגיל החדש", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		detailsPanel.setLayout(new SpringLayout());
		textField_MenuId = new JTextField();
		textField_MenuId.setHorizontalAlignment(JTextField.RIGHT);
		textField_MenuId.setFont(new Font("Tahoma", Font.PLAIN, 16));
		
		MessageShowIDStandardMenu msgShowID=new MessageShowIDStandardMenu( );
		try {
			mainMenu.ms.client.sendToServer(msgShowID);
			MessageShowIdStandardMenuReply resultID=(MessageShowIdStandardMenuReply)mainMenu.ms.client.GetMessage();
		    mId=Integer.parseInt(resultID.getResultID());	
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		 mId=mId+1;
		textField_MenuId.setText(Integer.toString(mId));
		textField_MenuId.setColumns(10);
		textField_MenuId.setEditable(false);
		textField_MenuId.setBackground(Color.pink);
		
		JLabel mid = new JLabel("מספר תפריט:", JLabel.TRAILING);
		mid.setForeground(new Color(75, 0, 130));
		mid.setFont(new Font("Tahoma", Font.BOLD, 12));
		mid.setLabelFor(textField_MenuId);
		
		detailsPanel.add(textField_MenuId);
		detailsPanel.add(mid);
		
		textField_nameMenu = new JTextField();
		textField_nameMenu.setHorizontalAlignment(JTextField.RIGHT);
		textField_nameMenu.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_nameMenu.setColumns(10);	
		
		JLabel name = new JLabel("שם תפריט:", JLabel.TRAILING);
		name.setForeground(new Color(75, 0, 130));
		name.setFont(new Font("Tahoma", Font.BOLD, 12));
		name.setLabelFor(textField_nameMenu);	
		//======================================================CHECK FOR CORRECT INPUT NAME==================================================
		KeyListener keyListener1 = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				JTextField nameField1 = (JTextField)arg0.getSource();
				String selectedName1 = (String)nameField1.getText();
				if(selectedName1.length()==0)
				{
					textField_nameMenu.setBackground(Color.white);
				}
				for(int i=0;i<selectedName1.length();i++)
				{
				
					textField_nameMenu.setBackground(Color.pink);
				
				}
			}

			public void keyTyped(KeyEvent arg0) {}		
		};	
		textField_nameMenu.addKeyListener(keyListener1);		
		
		//======================================================================================================================
		
		detailsPanel.add(textField_nameMenu);
		detailsPanel.add(name);
		
		
	   	
		//===========================================CHECK FOR CORRECT INPUT status==================================================
		
		
		 statusMenu=new JComboBox(new ConstantObjects().getMenuStatus());
		statusMenu.addActionListener(new java.awt.event.ActionListener() 
		  	{
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JComboBox roleBox = (JComboBox)e.getSource();
				    String selectedRole = (String)roleBox.getSelectedItem(); 
				    if(selectedRole.equals("פעיל") || selectedRole.equals("לא פעיל"))
				    {
				    	if(selectedRole.equals("פעיל"))
				    		flag="פעיל";
				    	else
				    		flag="לא פעיל";
				    					  
				    	statusMenu.setBackground(Color.pink);
				  
			    	}
				    else
				    {
				    	
				    	statusMenu.setBackground(Color.RED);
				    }
				
					} 
			});	
		
		//============================================================================================================================
		
			JLabel label_1 = new JLabel("סטטוס:", JLabel.TRAILING);
			label_1.setForeground(new Color(75, 0, 130));
			label_1.setFont(new Font("Tahoma", Font.BOLD, 12));
			label_1.setLabelFor(statusMenu);
			detailsPanel.add(statusMenu);
			detailsPanel.add(label_1);	
		
		
		SpringUtilities.makeCompactGrid(detailsPanel, 3, 2, //rows, cols
			     100, 50, //initX, initY
			        10, 50); //xPad, yPad
		
	return detailsPanel;
	}
	
	
	
	
	private int inputCheck()
	 {
		int result=0;
		
		
		//=================================================check for empty price and name================================		
		
		
		
		String name=textField_nameMenu.getText();
		String selectedRole = (String)statusMenu.getSelectedItem(); ; 
		
			if(name.isEmpty())
			{
				JOptionPane.showMessageDialog(null,"יש לתת שם לתפריט");	
				textField_nameMenu.setBackground(Color.RED);
				result=1;
			}
		
		
		//=================================================check for status================================		
		if(result==0)
		{
		if(selectedRole.equals("פעיל") || selectedRole.equals("לא פעיל"))
	    {
	    	
	    	statusMenu.setBackground(Color.pink);
	    		    	
    	}
	    else
	    {
	    	JOptionPane.showMessageDialog(null,"יש לבחור סטטוס");	
	    	statusMenu.setBackground(Color.RED);
	    	result=1;
	    }
		
		}
		return result;
}

}
